$(function (){ 
	//从topicType.do的getData取得树节点。
	$("#cancelButt").bind("click",function(){
		$("#addNodeInfo").removeClass("printDiv");
		$("#helpInforDiv").removeClass("hiddenDiv");
	});
	
	$("#addNodeInfo").addClass("hiddenDiv");
	
	$("#basic_json_2").tree({
		data : { 
			type : "json",
			opts : {
			method:"POST",
				url:"./topicType.do?action=getData"
			},
			async:false
			//async_data :function (NODE) { 
			//	return { id : $(NODE).attr("id") || 0 }
			//}
		},
		plugins : { 
			contextmenu : {
				items : {
					create : {
						label	: "新建下级菜单", 
						icon	: "create",
						visible	: function (NODE, TREE_OBJ) { 
							if(NODE.length != 1) return 0; 
							return TREE_OBJ.check("creatable", NODE); 
						}, 
						action	: function (NODE, TREE_OBJ) { 
							//新建节点
							//$.tree_reference("basic_json_2").refresh();
							
							//TREE_OBJ.create(false, TREE_OBJ.get_node(NODE[0])); 
							$("#typeName").val("");
		 					$("#typeEnlishName").val("");
		 					$("#typeDesc").val("");
		 					$("#typePageKey").val("");
		 					$("#typePageTitle").val("");
		 					$("#operateTitle").html("<p>*新增下级菜单</p>"); 
		 					$("#helpInforDiv").addClass("hiddenDiv");
		 					$("#addNodeInfo").removeClass("printDiv");
							$("#addNodeInfo").addClass("printDiv");
							$("#addNodeButt").unbind("click");
							$("#addNodeButt").bind("click",function (){
								
								var typeName = $("#typeName").val();
								var typeEnlishName = $("#typeEnlishName").val();
								var typeDesc = $("#typeDesc").val();
								var typePageKey = $("#typePageKey").val();
								var typePageTitle = $("#typePageTitle").val();
								var dataStr = "pid=" + $(NODE).attr("id") +
									"&typeName=" + typeName +
									"&typeEnlishName=" +typeEnlishName+
									"&typeDesc=" +typeDesc+
									"&typePageKey=" +typePageKey+
									"&typePageTitle=" +typePageTitle;
								$.ajax({
	 								type: "POST",
	 								url: "./topicType.do?action=addNode",
	 								data: dataStr,
	 								success: function(msg){ 
		 								TREE_OBJ.refresh();
		 								$("#typeName").val("");
		 								$("#typeEnlishName").val("");
		 								$("#typeDesc").val("");
		 								$("#typePageKey").val("");
		 								$("#typePageTitle").val("");
		 								$("#addNodeInfo").removeClass("printDiv");
		 								$("#helpInforDiv").removeClass("hiddenDiv");
	 								} ,
	 								complete : function(){
					 				}
								}); 
							});
						},
						separator_after : true
					},
					rename : {
						label	: "修改菜单信息", 
						icon	: "rename",
						visible	: function (NODE, TREE_OBJ) { 
							if(NODE.length != 1) return false; 
							return TREE_OBJ.check("renameable", NODE); 
						}, 
						action	: function (NODE, TREE_OBJ) { 
							$("#operateTitle").html("<p>*修改菜单信息</p>"); 
							$("#helpInforDiv").addClass("hiddenDiv");
							$("#addNodeInfo").addClass("printDiv");
							//获取节点数据
							$.ajax({
	 								type: "POST",
	 								url: "./topicType.do?action=getNodeInfor",
	 								data:"id=" + $(NODE).attr("id") ,
	 								async:true,
	 								success: function(msg){ 
	 									//alert(msg);
	 									//alert(returnVlaue.typeEnlishName);
	 									eval("var returnValue ="+msg);
	 									$("#typeName").val(returnValue.typeName);
		 								$("#typeEnlishName").val(returnValue.typeEnlishName);
		 								$("#typeDesc").val(returnValue.typePageDesc);
		 								$("#typePageKey").val(returnValue.typePageKey);
		 								$("#typePageTitle").val(returnValue.typePageTitle);
	 								} ,
	 								complete : function(){
					 				}
								}); 
							//提交事件
							$("#addNodeButt").unbind("click");
						
							$("#addNodeButt").bind("click",function subData(){
								
								var typeName = $("#typeName").val();
								var typeEnlishName = $("#typeEnlishName").val();
								var typeDesc = $("#typeDesc").val();
								var typePageKey = $("#typePageKey").val();
								var typePageTitle = $("#typePageTitle").val();
								var dataStr = "id=" + $(NODE).attr("id") +
									"&typeName=" + typeName +
									"&typeEnlishName=" +typeEnlishName+
									"&typeDesc=" +typeDesc+
									"&typePageKey=" +typePageKey+
									"&typePageTitle=" +typePageTitle;
								$.ajax({
	 								type: "POST",
	 								url: "./topicType.do?action=updateNode",
	 								data: dataStr,
	 								success: function(msg){ 
		 								TREE_OBJ.refresh();
		 								$("#addNodeInfo").removeClass("printDiv");
		 								$("#typeName").val("");
		 								$("#typeEnlishName").val("");
		 								$("#typeDesc").val("");
		 								$("#typePageKey").val("");
		 								$("#typePageTitle").val("");
		 								$("#addNodeInfo").removeClass("printDiv");
		 								$("#helpInforDiv").removeClass("hiddenDiv");
	 								} ,
	 								complete : function(){
					 				}
								}); 
							})
						} 
					},
					remove : {
						label	: "删除菜单",
						icon	: "remove",
						visible	: function (NODE, TREE_OBJ) { 
							var ok = true; 
							$.each(NODE, function (){ 
								if(TREE_OBJ.check("deletable", this) == false) {
									ok = false; 
									return false; 
								}
							}); 
							return ok; 
						}, 
						action	: function (NODE, TREE_OBJ) {
							//弹出提示
							if(confirm("确定删除?")) {
								$.each(NODE, function (){ 
									TREE_OBJ.remove(this); 
								}); 
							}
						} 
					}
				}
			}
		},
		callback : {
			onclick:function(NODE,TREE_OBJ){
				$("#addNodeInfo").removeClass("printDiv");
			},
			onrename : function (NODE, TREE_OBJ) {
				
				//发送修改请求。
				var title = TREE_OBJ.get_text(NODE);
				$.ajax({
 					type: "POST",
 					url: "./topicType.do?action=updateNode",
 					data:"id=" + $(NODE).attr("id")+"&text="+TREE_OBJ.get_text(NODE) ,
 					success: function(msg){ 
 						TREE_OBJ.refresh();
 					} ,
 					complete : function(){
 					}
				}); 
			},
			ondelete : function (NODE,TREE_OBJ,r) {
				$.ajax({
 					type: "POST",
 					url: "./topicType.do?action=deleteNode",
 					data: "id=" + $(NODE).attr("id"),
 					success: function(msg){ 
 					} ,
 					complete : function(){
 					}
				}); 
			},
			oncreate : function (NODE, REF_NODE, TYPE, TREE_OBJ, RB) {
			}
		}
	});
});